package com.gking.centerSystem.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gking.centerSystem.AOP.annotationUtil.desensitizeData.DesensitizeData;
import com.gking.centerSystem.AOP.annotationUtil.desensitizeData.SensitizedTypeEnum;
import com.gking.centerSystem.common.BaseContext;
import com.gking.centerSystem.common.Result;
import com.gking.centerSystem.entity.dao.PagingQueryUserDao;
import com.gking.centerSystem.entity.dto.UserRoleListDto;
import com.gking.centerSystem.entity.Routes;
import com.gking.centerSystem.entity.User;
import com.gking.centerSystem.service.SystemInfo.SystemInfoService;
import com.gking.centerSystem.service.User.UserService;
import com.gking.centerSystem.service.UserRoles.UserRolesService;
import com.gking.centerSystem.utils.GetAuthInfoUtil;
import com.gking.centerSystem.utils.LimitAccessUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    @Resource
    private UserRolesService userRolesService;

    @Resource
    private SystemInfoService systemInfoService;


    /**
     * 获取用户登录缓存的凭证信息 (设置访问ip白名单)
     * 这里本来要用post请求，但是遇到 HTTP 请求的输入流只能读取一次的情况，所以只能用get请求
     *
     * @return s
     */
    @GetMapping("/limit/loginAuth")
    public Result<Map> getUserLoginAuth(HttpServletRequest request, @RequestParam String ip) {
        Result<List<String>> systemIpList = systemInfoService.getSystemIpList();
        log.info("设置访问ip白名单: {} ", systemIpList.getData());
        boolean b = LimitAccessUtil.remoteSubAccess(GetAuthInfoUtil.getClientIp(request), systemIpList.getData());
        if (!b) return Result.error("无法访问！");

        try {
            return userService.getUserLoginAuth(request.getHeader("c-device-id"), ip);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    /**
     * 退出登录
     *
     * @return s
     */
    @RequiresPermissions("CS:user:logout")
    @PostMapping("/logout")
    public Result<String> logout() {
        try {
            return userService.logout();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }


    /**
     * 获取用户信息
     *
     * @return s
     */
    @RequiresPermissions("CS:user:info:get")
    @GetMapping("/info")
    public Result<User> getUserInfo() {
        try {
            return userService.getUserInfo();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }


    /**
     * 修改用户信息
     *
     * @param user 用户信息
     * @return s
     */
    @RequiresPermissions("CS:user:info:update")
    @PutMapping("/updateInfo")
    public Result<String> updateUserInfo(@RequestBody User user) {
        String nickname = user.getNickname();
        String phoneNumber = user.getPhoneNumber();
        String email = user.getEmail();
        String signature = user.getSignature();

        try {
            return userService.updateUserInfo(nickname, phoneNumber, email, signature);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }


    /**
     * 获取用户角色详情
     *
     * @return s
     */
    @RequiresPermissions("CS:user:info:getRoles")
    @GetMapping("/getUserRoles")
    public Result<UserRoleListDto> getUserRolesDetail() {
        String userId = BaseContext.getCurrentId();
        try {
            return userRolesService.getUserRolesDetail(userId);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    /**
     * 获取用户可以访问的页面路由
     *
     * @param systemName 系统名称
     * @return s
     */
    @RequiresPermissions("CS:user:info:getRoutes")
    @GetMapping("/getAccessRoutes")
    public Result<List<Routes>> getUserAccessRoutes(@RequestParam String systemName) {
        try {
            return userService.getUserAccessRoutes(systemName);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }


    /**
     * 分页条件查询用户信息
     *
     * @param page               分页页码
     * @param size               分页大小
     * @param pagingQueryUserDao 分页查询用户的一些条件
     * @return s
     */
    @DesensitizeData({
            @DesensitizeData.Field(field = "email", type = SensitizedTypeEnum.EMAIL),
            @DesensitizeData.Field(field = "phoneNumber", type = SensitizedTypeEnum.PREFIX),
    })
    @RequiresPermissions("CS:manager:query:user")
    @PostMapping("/queryPageUserList")
    public Result<Page> queryPageUserList(@RequestParam Integer page, @RequestParam Integer size,
                                          @RequestBody PagingQueryUserDao pagingQueryUserDao) {
        log.warn("分页查询用户的一些条件： {} ", pagingQueryUserDao);

        try {
            return userService.queryPageUserList(page, size, pagingQueryUserDao);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

    /**
     * 分页条件查询用户信息
     *
     * @param page 分页页码
     * @param size 分页大小
     * @param user 分页查询用户的一些条件
     * @return s
     */
    @DesensitizeData({
            @DesensitizeData.Field(field = "email", type = SensitizedTypeEnum.EMAIL),
            @DesensitizeData.Field(field = "phoneNumber", type = SensitizedTypeEnum.PREFIX),
    })
    @RequiresPermissions("CS:manager:query:user")
    @PostMapping("/queryPageUserByEasy")
    public Result<Page> queryPageUserByEasy(@RequestParam Integer page, @RequestParam Integer size,
                                            @RequestBody User user) {
        String account = user.getAccount();
        String nickname = user.getNickname();

        try {
            return userService.queryPageUserByEasy(page, size, account, nickname);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }


    /**
     * 设置用户状态
     *
     * @param userIds 用户id
     * @param status  状态 0 、1
     * @return s
     */
    @DesensitizeData(needVerify = true)
    @RequiresPermissions("CS:manager:set:user")
    @PostMapping("/setUserStatus")
    public Result<String> setUserStatus(@RequestBody List<String> userIds, @RequestParam String status) {
        try {
            return userService.setUserStatus(userIds, status);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }
    }

}
